﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using IPhonePlugin;

namespace RutTrichThongTin
{
    public class PhoneFeederManager
    {
        public PhoneFeederManager()
        {
            _ListPhonePlugin = new List<IPhonePlugin.IPhonePlugin>();
        }

        private List<IPhonePlugin.IPhonePlugin> _ListPhonePlugin;

        public List<IPhonePlugin.IPhonePlugin> ListPhonePlugin
        {
            get { return _ListPhonePlugin; }
            set { _ListPhonePlugin = value; }
        }


        public List<IPhonePlugin.PhoneInfo> GetSearchResult(string keyword)
        {
            int n=_ListPhonePlugin.Count;
            List<PhoneInfo> listPhone = new List<PhoneInfo>();
            for (int i = 0; i < n; i++)
            {
                List<PhoneInfo> temp = _ListPhonePlugin[i].getPhone(keyword, 10);
                if (temp != null && temp.Count > 0)
                {
                    int m = temp.Count;
                    for (int j = 0; j < m; j++)
                    {
                        listPhone.Add(temp[j]);
                    }
                }
            }
            return listPhone;
        }

        public void LoadFeederLibrary(string path)
        {

            IPhonePlugin.IPhonePlugin temp = null;
            _ListPhonePlugin.Clear();

            string[] files = System.IO.Directory.GetFiles(path + @"\AddOn\", "*.dll");

            foreach (string f in files)
            {
                Assembly asm = Assembly.LoadFile(f);
                Type[] types = asm.GetTypes();
                foreach (Type t in types)
                {
                    if (typeof(IPhonePlugin.IPhonePlugin).IsAssignableFrom(t))
                    {
                        temp = Activator.CreateInstance(t) as IPhonePlugin.IPhonePlugin;
                        _ListPhonePlugin.Add(temp);
                    }
                }
            }
        }
    }
}